<template>
	<main-box id="grade_sort" :vtitle="title">
		<div class="condition">
			<div class="right_btn" @click="openAdd()">新增学年</div>
		</div>
		<div>
			<table class="table" cellspacing="0">
				<thead class="thead">
					<tr>
						<td style="width: 2.5rem;">学年名称</td>
						<td style="width: 4rem;">学年起止日期</td>
						<td style="width: 4rem;">上学期起止日期</td>
						<td style="width: 4rem;">下学期起止日期</td>
						<td style="width: 1.7rem;">是否当前学年</td>
						<td style="width: 1.8rem;">操作</td>
					</tr>
				</thead>
				<tbody class="tbody">
					<tr v-for="item,index in schoolYearList">
						<td>{{item.name}}</td>
						<td>{{item.yearTime}}</td>
						<td>{{item.firstTime}}</td>
						<td>{{item.secondTime}}</td>
						<td>{{item.isCurSchoolYear==1 ? '是':'否'}}</td>
						<td>
							<span class="operate_btn" @click="openEdit(item)">编辑</span>
							&nbsp;
							<span class="operate_btn" @click="toDelete(item.schoolYearId)">删除</span>
						</td>
					</tr> 
				</tbody>
			</table>
		</div>
		<el-pagination
	    	layout="total, sizes, prev, pager, next, jumper"
	    	:current-page="param.currPage"
	    	@size-change="pageSizeChange"
      		@current-change="pageIndexChange"
	    	:total="count">
	    </el-pagination>
		
		<schoolYearAddEdit ref="schoolYearAddEdit" @callback="closeAddEdit"></schoolYearAddEdit>
	</main-box>
</template>

<script>
	
import schoolYearAddEdit from './common/school_year_add_edit'
	
var vm,dict;
export default {
	components : {
		schoolYearAddEdit
	},
	data : function() {
		return {
			title : this.$route.name,
			param : {
				campusId : this.$store.state.campusId,
				currPage : 1,
				pageSize : 10
			},
			count : null,
	        schoolYearList : [],
		}
	},
	methods : {
		search : function(currPage) {
			vm.param.currPage = currPage || 1;
			
			api.schoolYearList(vm.param).then(function(data) {
				//处理要显示的日期字符串
				for(let item of data.list) {
					item.yearTime = vm.handleStr(item.startTime, item.endTime);
					item.firstTime = vm.handleStr(item.firstStartTime, item.firstEndTime);
					item.secondTime = vm.handleStr(item.secondStartTime, item.secondEndTime);
				}
				vm.schoolYearList = data.list;
				vm.count = data.totalCount;
			})
		},
		handleStr : function(str1, str2) {
			var temp = "yyyy年MM月dd日";
			let date1 = new Date(str1)
			str1 = tool.DateToString(new Date(str1), temp);
			str2 = tool.DateToString(new Date(str2), temp);
			return str1 + "-" + str2;
		},
		campusChange : function(campusId) { //校区改变
			vm.param.campusId = campusId;
			vm.search();
		},
		pageSizeChange : function(val) { //页条数发生改变
	  		vm.param.pageSize = val;
	    	vm.search();
	  	},
	  	pageIndexChange : function(val) { //页码发生改变
	    	vm.param.currPage = val;
	    	vm.search(val);
	  	},
	  	toDelete : function(id) {
	  		tool.confirm("确认删除?", {
        		confirmButtonText: "确定",
        		cancelButtonText: "取消",
        		type: 'warning'
       	 	}).then(function() {
          		api.schoolYearDelete({id}).then(function(data) {
		  			vm.search();
		  			tool.message({ showClose: true, message: "删除成功", type: 'success' });
		  		})
        	});
	  		
	  	},
		openAdd : function() {
			vm.$refs.schoolYearAddEdit.init();
		},
		openEdit : function(schoolYear) {
			vm.$refs.schoolYearAddEdit.init(schoolYear);
		},
		closeAddEdit : function() {
			vm.search();
		}
	},
	mounted : function() {
		
	},
	created : function() {
		vm = this;
		//请求列表数据
		vm.search();
		
		//获取数据字典对象
		dict = tool.getItem(lsKey.dict,true);
	}
}

</script>

<style lang="scss">
	#charge_type {
		.el-input__inner {
			border-radius: 0.08rem;
			font-size: 0.16rem;
			color: #666666;
			height: 0.36rem;
			line-height: 0.36rem;
		}
		.el-input__icon {
			line-height: 0.36rem;
		}
		
		.input_search {
			.el-input__inner {
				background-color: rgb(247,248,250);
			}
		}
	}
</style>
<style scoped src="@/style/module/table.css"></style>
<style scoped>
	.right_btn {
		background-image: url(~@/assets/img/clue_manage/add_white.png);
	}
	/*条件*/
	.condition {
		padding-bottom: 0.2rem;
		height: 0.58rem;
	}
</style>